import {
  isEMail
} from '../../../api/commons';
// pages/under-door/surveyed/index.js
// 已调查
import api from '../../../api/index';
import {
  uploadFileTest
} from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    tbSurveyData: {},
    tbSurveyAccessoryList: [],
    surveyConfirmList:[],//勘察确认图片
    tbSurvey:{},
    newNote:'',//修改的备注
    isClick:true,//是否可以点击修改,
    isRealSurveer:false,//是否为真下户人员   控制显示勘察照片权限
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.setData({
      id: options.id,
      isRealSurveer:JSON.parse(options.isRealSurveer)
    })
    console.log(this.data.isRealSurveer)
    if (this.data.id) {
      this.getDetail();
    }
  },
  //修改防止重复点击
  
  updateInfo(){
      if(this.data.isClick){
        this.updateNote();
      }else{
        wx.showToast({
          icon:'none',
          title: '休息会儿在点吧',
        })
      }

  },
  //修改备注
  updateNote(){
    if(this.data.newNote!=this.data.tbSurvey.note){
      api.updateNote({
        id:this.data.id,
        note:this.data.newNote
      }).then(res=>{
        wx.showToast({
          title: '修改成功',
        })
        this.setData({
          isClick:false,
          [`tbSurvey.note`]:this.data.newNote
        })
        let timer=setTimeout(()=>{
          this.setData({
            isClick:true
          })
          clearTimeout(timer);
        },5000)
      })
    }
  },

  //输入备注
  bindinput(e){
    console.log(e);
    let value=e.detail. value;
    this.setData({
      newNote:value
    })
  
  },
  //删除图片
  delImg(e) {
    let {
      index
    } = e.currentTarget.dataset;
    let data = this.data.tbSurveyAccessoryList;
    api.deleteImg(data[index].id).then(res => {
      data.splice(index, 1);
      this.setData({
        tbSurveyAccessoryList: data
      })
    })

  },

  //保存数据
  addArriveImg2(obj) {
    return api.addArriveImg2(obj).then(res => {
      return res.data.data;
    })
  },

    //获取上传
    async upload(arr, num) {
      //num 8 勘察确认照片
        for (let index = 0; index < arr.length; index++) {
          let ossRes = await api.surveyGetMap();
          let credentials = ossRes.data.data;
          let itemArr = arr[index].path.split('.');
          let suffix = itemArr[itemArr.length - 1];
          let url = await uploadFileTest(arr[index].path, credentials, suffix);
          let obj = {
            url,
            type: num,
            size: arr[index].size,
            sid: this.data.id,
            showName: url,
          }
          let resData = await this.addArriveImg2(obj);
          console.log(resData)
          let data=this.data.tbSurveyAccessoryList;
          console.log('data',data);
          data.push(resData);
          console.log('data1',data);
          this.setData({
            tbSurveyAccessoryList:data
          })
        console.log('111',this.data.tbSurveyAccessoryList)
  
        }
  
      return true;
  
    },

  //选择图片
  chooseImg(e) {
    let _this = this;
    let {
      type
    } = e.currentTarget.dataset;
    wx.chooseImage({
      count:type==0?1:9,
      sizeType: ['original', 'compressed'],
      sourceType: ['camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFiles = res.tempFiles;
        // if (type == 0) {
        //   _this.upload(tempFiles[0], type)
        // } else {
          _this.upload(tempFiles, type)
        // }
       
      }
    })
  },


  //获取详情
  getDetail() {
    api.surveyDetail(this.data.id).then(res => {
      let tbSurveyData = {};
      let data = res.data.data.tbSurveyData;
      for (const key in data) {
        if (!data[key]) {
          tbSurveyData[key] = ''
        } else {
          tbSurveyData[key] = data[key]
        }
      }
      this.setData({
        tbSurveyData: tbSurveyData,
        tbSurveyAccessoryList: res.data.data.tbSurveyAccessoryList,
        tbSurvey:res.data.data.tbSurvey,
        newNote:res.data.data.tbSurvey.note
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})